﻿using UnityEngine;
using System.Collections;

public class BattleUtil
{

	public static Vector3 GetTargetAttackPos(Vector3 me, Vector3 target, float offset)
	{
		return TargetAttackPos(me, target, 0, offset, 1);
	}

	public static Vector3 GetTargetAttackPos(Vector3 me, Vector3 target, float offset, float offsetMulti)
	{
		return TargetAttackPos(me, target, 0, offset, offsetMulti);
	}

	//target방향으로 offset만큼 떨어진 지점//
	public static Vector3 GetPosToTarget(Vector3 me, Vector3 target, float offset)
	{
		Vector3 vDir = (target - me);
		Vector3 vResult = me + vDir.normalized * offset;
		return vResult;
	}

	//타겟 반대 방향으로 offset * multi 만큼 떨어진 위치//
	public static Vector3 TargetAttackPos(Vector3 me, Vector3 target, int atkCount, float _offset, float offsetMulti)
	{
		float offset = _offset * offsetMulti;
		Vector3 vDir = -(target - me);
		if(atkCount==1)
			vDir = Quaternion.AngleAxis(30, Vector3.up) * vDir;
		else if(atkCount==2)
			vDir = Quaternion.AngleAxis(-30, Vector3.up) * vDir;
		else if(atkCount==3)
			vDir = Quaternion.AngleAxis(-60, Vector3.up) * vDir;
		Vector3 vResult = target + vDir.normalized * offset;
		return vResult;
	}

	public static void LookAt(Transform me, Transform target)
	{
		me.LookAt(target);
	}

	public static int GetDividedDamage(int _totalDamage, int hitCount, int totalHit)
	{
		if(hitCount<=0 || hitCount>totalHit || totalHit<1)
		{
			Debug.LogError("GetDividedDamage error "+hitCount+"  "+totalHit);
			return 1;
		}

		if(totalHit==1)
			return _totalDamage;

		int [] damage = new int[totalHit];
		int totalDam = 0;
		for(int i=0; i< totalHit; i++) {
			damage[i] = (int) ( _totalDamage * 1.0f/totalHit);
			totalDam += damage[i];
		}
		int remainDam = _totalDamage - totalDam;
		if(remainDam>0)
			damage[totalHit-1] += remainDam;

		//Debug.LogError("dam total ="+_totalDamage+"  damage="+damage[hitCount-1]);
		return damage[hitCount-1];
	}

	public static void ClearTransformLocal(Transform _trans)
	{
		_trans.localPosition = Vector3.zero;
		_trans.localRotation = Quaternion.identity;
		_trans.localScale = Vector3.one;
	}

	public static Transform SearchHierarchyForBone(Transform current, string name)   
	{
		// check if the current bone is the bone we're looking for, if so return it
		if (current.name == name)
			return current;
		
		// search through child bones for the bone we're looking for
		for (int i = 0; i < current.GetChildCount(); ++i)
		{
			// the recursive step; repeat the search one step deeper in the hierarchy
			Transform found = SearchHierarchyForBone(current.GetChild(i), name);
			
			// a transform was returned by the search above that is not null,
			// it must be the bone we're looking for
			if (found != null)
				return found;
		}
		
		// bone with name was not found
		return null;
	}
}

public static class UnityExtensionMethods {
	/// <summary>
	/// Determines whether the quaternion is safe for interpolation or use with transform.rotation.
	/// </summary>
	/// <returns><c>false</c> if using the quaternion in Quaternion.Lerp() will result in an error (eg. NaN values or zero-length quaternion).</returns>
	/// <param name="quaternion">Quaternion.</param>
	public static bool IsValid(this Quaternion quaternion)
	{
		bool isNaN = float.IsNaN(quaternion.x + quaternion.y + quaternion.z + quaternion.w);
		
		bool isZero = quaternion.x == 0 && quaternion.y == 0 && quaternion.z == 0 && quaternion.w == 0;
		
		return !(isNaN || isZero);
	}
}
